// SEE END OF FILE FOR LICENSE TERMS

#include "Database_functions.h"
#include "Database_defines.h"

// Write data to the database
// NO NV MEMORY SUPPORT YET.
// NO WRITE FUNCTION SUPPORT YET.
// NOT TESTED NOT TESTED NOT TESTED 
unsigned int DBWriteData(unsigned int idRef, void* pData, unsigned char arrayCount, unsigned char arrayOffset)
{
	DBaseEntry* pDBEntry = (DBaseEntry*) &Database[idRef];
	
	// If there is no write access return zero
	if(pDBEntry->writeAccess == 0) return 0;

	// ADD WRITE FUNCTION ACCESS SOMEWHERE HERE

	// If the RAM pointer is NULL, return zero.
	if(pDBEntry->pRAM == 0) return 0;

	// If the requested write is too big, return zero
	if( (arrayCount + arrayOffset) > pDBEntry->arraySize) return 0;

	// Type size multiplies by array size.  offset is subtracteed to find remainder of size.
	unsigned int	TypeSize	= dataTypeSizes[pDBEntry->type];
	unsigned int	DataOffset	= (TypeSize * arrayOffset);
	unsigned int 	DataSize 	= (TypeSize * arrayCount);

	memcpy(pDBEntry->pRAM + DataOffset, pData, DataSize);
	return DataSize;
};


// Read data from the database
// NO NV MEMORY SUPPORT YET.
// NOT TESTED NOT TESTED NOT TESTED 
unsigned int DBReadData(unsigned int idRef, void* pData, unsigned char arrayCount, unsigned char arrayOffset)
{
	DBaseEntry* pDBEntry = (DBaseEntry*) &Database[idRef];
	
	// If there is no read access, return zero
	if(pDBEntry->readAccess == 0) return 0;

	// If there is no pointer to RAM, return zero
	if(pDBEntry->pRAM == 0) return 0;

	// If the requested read is too big, return zero
	if( (arrayCount + arrayOffset) > pDBEntry->arraySize) return 0;

	// Type size multiplies by array size.  offset is subtracteed to find remainder of size.
	unsigned int	TypeSize	= dataTypeSizes[pDBEntry->type];
	unsigned int	DataOffset	= (TypeSize * arrayOffset);
	unsigned int 	DataSize 	= (TypeSize * arrayCount);

	memcpy(pData, pDBEntry->pRAM + DataOffset, DataSize);
	return DataSize;
};

// WriteBits may not be supportable. Use a write function instead.
// Not supported yet
/*
unsigned int DBWriteBits(unsigned int idRef, unsigned int setMask, unsigned int clearMask, unsigned int offset)
{
	return 0;

};
*/

unsigned int DBGetDataSize(unsigned int idRef)
{
	DBaseEntry* pDBEntry = &Database[idRef];
	return (dataTypeSizes[pDBEntry->type] * pDBEntry->arraySize );
};

unsigned int DBGetDataArraySize(unsigned int idRef, unsigned char arrayCount, unsigned char arrayOffset)
{
	DBaseEntry* pDBEntry = &Database[idRef];
	unsigned int arraySize = pDBEntry->arraySize;
	
	// Return fault if requested size is too large.
	if( (arrayCount + arrayOffset) > arraySize) return 0;

	return (dataTypeSizes[pDBEntry->type] * arrayCount);
};


unsigned int DBGetArraySize(unsigned int idRef)
{
	return Database[idRef].arraySize;
};

unsigned int DBGetArrayCountFromByteCount(unsigned int idRef, unsigned int byteCount)
{	
	unsigned int count = 0;
	int remainingBytes = byteCount;

	if(idRef == 0) return 0;

	DBaseEntry* pDBEntry = &Database[idRef];
	unsigned int typeSize = dataTypeSizes[pDBEntry->type];

	if(typeSize == 0) return 0;		// Sanity check for zero size;

	while(remainingBytes >= typeSize)
	{
		remainingBytes -= typeSize;
		count++;
	};

	return count;	
};


unsigned int DBGetTypeSize(unsigned int idRef)
{
	return dataTypeSizes[Database[idRef].type];
};


/****************************************************************************/
// This is part of the servo and radio interface software
//
// ServoInterface source code
//	http://code.google.com/p/rc-servo-interface
//
// Copyright 2010 ServoInterface Team
// See the AUTHORS.TXT file for a list of authors of ServoInterface.
//
// ServoInterface is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ServoInterface is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License 
// along with ServoInterface.  If not, see <http://www.gnu.org/licenses/>.
//
// Many parts of ServoInterface use either modified or unmodified code
// from the MatrixPilot pilot project.
// The project also contains code for modifying MatrixPilot to operate
// with ServoInterface.
// For details, credits and licenses of MatrixPilot see the AUTHORS.TXT file.
// or see this website: http://code.google.com/p/gentlenav
/****************************************************************************/
